package cn.com.surker.pages.console;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.common.AbstractController;
import cn.com.surker.enums.material.MaterialStoreTypeEnum;
import cn.com.surker.material.entity.MainMaterial;
import cn.com.surker.material.service.MainMaterialService;
import cn.com.surker.system.entity.SysMenuRoleController;
import cn.com.surker.system.entity.SysOrigin;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.system.service.SysLineService;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/console/system")
public class SystemIndexController extends AbstractController {

    @Resource
    private SysLineService sysLineService;
    @Resource
    private MainMaterialService materialService;

    /**
     * 工控仓位对应关系表页面跳转
     * @return
     */
    @RequestMapping("/storage-match")
    public ModelAndView storageMatch() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
        if (ObjectUtil.isEmpty(user)) {//未登录用户
            modelAndView.setViewName("console/system/login");
            return modelAndView;
        }
        List<Map<String, Object>> storageList = new ArrayList<>();
        for (int i = 0; i < MaterialStoreTypeEnum.values().length;i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialStoreTypeEnum.values()[i].getCode());
            map.put("name", MaterialStoreTypeEnum.values()[i].getDescription());
            storageList.add(map);
        }
        List<SysOrigin> orgList = getUserOrgList();
        List<SysLine> lineList = sysLineService.findAll();
        SysMenuRoleController menuRole = isControlVerify("/console/system/storage-match");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("lineList", lineList);
        modelAndView.addObject("storageList", storageList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/system/storage-match");
        return modelAndView;
    }

    /**
     * 仓位信息页面跳转
     * @return
     */
    @RequestMapping("/storage")
    public ModelAndView storage() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
        if (ObjectUtil.isEmpty(user)) {//未登录用户
            modelAndView.setViewName("console/system/login");
            return modelAndView;
        }
        List<Map<String, Object>> storageList = new ArrayList<>();
        for (int i = 0; i < MaterialStoreTypeEnum.values().length;i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialStoreTypeEnum.values()[i].getCode());
            map.put("name", MaterialStoreTypeEnum.values()[i].getDescription());
            storageList.add(map);
        }
        List<SysOrigin> orgList = getUserOrgList();
        List<MainMaterial> materialList = materialService.getListMaterialByOrgId(orgList.get(0).getId());
        SysMenuRoleController menuRole = isControlVerify("/console/system/storage");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("materialList", materialList);
        modelAndView.addObject("storageList", storageList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/system/storage");
        return modelAndView;
    }

    /**
     * 生产线页面跳转
     * @return
     */
    @RequestMapping("/sysline")
    public ModelAndView sysline() {
        ModelAndView modelAndView = new ModelAndView();
//        SysUser user = getUser();
//        if (ObjectUtil.isEmpty(user)) {//未登录用户
//            modelAndView.setViewName("console/system/login");
//            return modelAndView;
//        }
        SysMenuRoleController menuRole = isControlVerify("/console/system/sysline");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        List<SysOrigin> orgList = getUserOrgList();
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/system/sysLine");
        return modelAndView;
    }

	@RequestMapping("/thread")
	public ModelAndView thread() {
		ModelAndView modelAndView = new ModelAndView();
		if (!isAdmin()) {
			modelAndView.setViewName("error/403");
		} else {
			modelAndView.setViewName("console/system/thread-list");
		}
		return modelAndView;
	}
}
